﻿
using RM.BasicLibrary.Exceptions;
using RM.BasicLibrary.Iocs;
using RM.BasicLibrary.Logs;
using RM.BasicLibrary.Models;
using RM.BasicLibrary.Serializes;
using RM.BasicLibrary.Resources;
using RM.BasicLibrary.Transfers.TCP;
using RM.BasicLibrary.Cryptographys;
using RM.BasicLibrary.Models.Databases;
using RM.BasicLibrary.Models.Repositorys;
using RM.BasicLibrary.Utils;
using RM.BasicLibrary.Caches;
#if NET46 || NET45
using RM.BasicLibrary.Models.Databases;
#endif

namespace RM.BasicLibrary
{
    /// <summary>
    /// 实例引导程序
    /// </summary>
    public class Root
    {
        static Root()
        {
            BasicIns.SetLog("Log/BasicLog", "yyyy-MM-dd'_BasicEX.html'", repositoryName: "_BasicLog");
        }
        /// <summary>
        /// 程序集内部实例引导程序
        /// </summary>
        internal static Bootstraps BasicIns = new Bootstraps();
        /// <summary>
        /// 默认初始化实例 Bootstraps 引导程序
        /// </summary>
        public static Bootstraps Ins = new Bootstraps();
        /// <summary>
        /// 常用静态类
        /// </summary>
        public static Util Util = new Util();
        /// <summary>
        /// IOC服务容器注册 提供企业库服务
        /// </summary>
        public static IIocManager ServerIns = new AutofacIocManager();
        /// <summary>
        /// 创建引导对象实例
        /// </summary>
        /// <returns></returns>
        public static Bootstraps CreateIns() { return new Bootstraps(); }
    }
    /// <summary>
    /// 脚本引导程序
    /// </summary>
    public class Bootstraps
    {
        /// <summary>
        /// 异常过滤器
        /// </summary>
        public IFilter ExFilter { get; set; }
        /// <summary>
        /// 异常处理器
        /// </summary>
        public IHandler ExHandler { get; set; }
        /// <summary>
        /// 数据库数据转实体处理器
        /// </summary>
        public IDataConvert DataConvert { get; set; }
        /// <summary>
        /// Sql语句生成器
        /// </summary>
        public ISqlBuilder SqlBuilder { get; set; }
        /// <summary>
        /// IOC容器管理类
        /// </summary>
        public IIocManager IocManager { get; set; }
        /// <summary>
        /// 编码所在的类和方法以及对应的帧数
        /// </summary>
        public CodeInfo CodeInfo { get; set; }
        /// <summary>
        /// 日记处理器
        /// </summary>
        public IBaseLog Log { get; set; }
        /// <summary>
        /// Json序列化
        /// </summary>
        public ISerialize JsonSerialize { get; set; }
        /// <summary>
        /// XML序列化
        /// </summary>
        public ISerialize XmlSerialize { get; set; }
        /// <summary>
        /// TCP服务端
        /// </summary>
        public ITcp TCPServer { get; set; }
        /// <summary>
        /// tcp客服端
        /// </summary>
        public ITcp TCPClient { get; set; }

        /// <summary>
        /// 获取多语言对象
        /// </summary>
        public Resource Resource { get; set; }
        /// <summary>
        /// HTTP请求
        /// </summary>
        public Transfers.Https.IHttp Http { get; set; }
        /// <summary>
        /// 对称加密
        /// </summary>
        public ICrypt DESCrypt { get; set; }
        /// <summary>
        /// 非对称加密
        /// </summary>
        public ICrypt RSACrypt { get; set; }

        /// <summary>
        /// sql ado.net处理器
        /// </summary>
        public IDatabase Database { get; set; }

        public IRepository Repository { get; set; }
        public ICache Cache { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        public Bootstraps() => Init();
        /// <summary>
        /// 默认初始化操作对象
        /// </summary>
        private void Init()
        {
            ExFilter = new ExceptionFilter();
            ExHandler = new ExceptionHandler();
            DataConvert = new DataConvert();
            SqlBuilder = new SqlBuilder();
            Log = new LogNet();
            IocManager = new AutofacIocManager();
            CodeInfo = new CodeInfo();
            JsonSerialize = new NewtonsoftJson();
            XmlSerialize = new XML();
            Resource = new Resource();
            TCPServer = new TcpServer();
            TCPClient = new TcpClient();
            Http = new Transfers.Https.Http();
            DESCrypt = new DESCrypt();
            RSACrypt = new RSACrypt();
            Database = new MSDatabase();
            Repository = new Repository(Database);
            Cache = new CacheByRedis();
#if NET46 || NET45
            Database = new MSDatabase();
#endif
        }
        #region public method
        /// <summary>
        /// 获取资源文件的值对象
        /// </summary>
        /// <param name="bootstraps"></param>
        /// <param name="key"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public string GetValue(string key, params object[] arguments)
        {
            return Resource.GetValue(key, arguments);
        }
        #endregion

    }
}


